using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaTrading
{
    //Table-Mapping Class (Customisable half)
    public partial class CSeries
    {
        #region Constants
        public const int DEFAULT_DP = 4;
        #endregion

        #region Constructors (Public)
        //Default Connection String
        public CSeries() : base() {}

        //Alternative Connection String
        public CSeries(CDataSrc dataSrc) : base(dataSrc) {}
        
        //Hidden  (UI code should use cache instead)
        protected internal CSeries(int seriesId) : base(seriesId) {}
        protected internal CSeries(CDataSrc dataSrc, int seriesId) : base(dataSrc, seriesId) {}
        protected internal CSeries(CDataSrc dataSrc, int seriesId, IDbTransaction txOrNull) : base(dataSrc, seriesId, txOrNull) { }
        #endregion

        #region Default Values
        protected override void InitValues_Custom()
        {
            _seriesDecimalPlaces = DEFAULT_DP;
       }
        #endregion
        
        #region Default Connection String
        protected override CDataSrc DefaultDataSrc()  { return CDataSrc.Default;  }
        #endregion

        #region Properties - Relationships
        //Relationships - Foriegn Keys (e.g parent)

        //Relationships - Collections (e.g. children)
        public CDataList Datas { get { return CData.Cache.GetBySeriesId(this.SeriesId); } }
        protected CDataList Datas_(IDbTransaction tx) {  return new CData(DataSrc).SelectBySeriesId(this.SeriesId, tx); } //Only used for cascade deletes        
        #endregion

        #region Properties - Customisation
        public double SeriesPointSize { get { return Math.Pow(0.1, SeriesDecimalPlaces); } }
        public double MeanPointValue { get { return SeriesPointSize / Datas.MeanClosing; } }    //Can be $ or %
        
        public int Length { get { return Datas.Count; } }
        public DateTime From { get { return Datas.MinDate; } }
        public DateTime To { get { return Datas.MaxDate; } }
        public double Total { get { return Datas.SimulatedTotal; } }
        public double Profit { get { return Datas.SimulatedProfit; } }
        public double Returns { get { return Datas.SimulatedReturns ; } }
        public double AnnualisedReturns { get { return Datas.AnnualisedReturns; } }
        public double AverageExposure { get { return Datas.AverageExposure; } }
        public double MaxExposure { get { return Datas.MaxExposure; } }       
        public double ExposureAdjustedReturns { get { return Datas.ExposureAdjustedReturns; } }
        public double StdDevClosing { get { return Datas.StdDevClosing; } }
        public double MeanClosing { get { return Datas.MeanClosing; } }
        public double VolatilityClosing { get { return Datas.VolatilityClosing; } }
        public int Wins { get { return Datas.Wins; } }
        public int Losses { get { return Datas.Losses; } }
        public int Flat { get { return Datas.Flat; } }
        public double Low { get { return Datas.Low; } }
        public double High { get { return Datas.High; } }
        #endregion

        #region Save/Delete Overrides
        public override void Delete(IDbTransaction txOrNull) 
        { 
            //Use a transaction if none supplied 
            if (txOrNull == null)
            { 
                BulkDelete(this); 
                return; 
            } 
            
            //Cascade-Delete (all child collections) 
            this.Datas_(txOrNull).DeleteAll(txOrNull);
            
            //Normal Delete 
            base.Delete(txOrNull); 
        }        
        #endregion

        #region Custom Database Queries
        //(Not normally required for cached classes, use list class for searching etc)
        //For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
        //For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
        #endregion

        #region Searching (Optional)
        //For cached classes, custom seach logic resides in static methods on the list class
        // e.g. CSeries.Cache.Search("...")

        //See also the auto-generated methods based on indexes
        //' e.g. CSeries.Cache.GetBy...
        #endregion

        #region Caching Details
        //Cache data
        private static CSeriesList LoadCache()  {   return new CSeries().SelectAll();   }
        //Cache Timeout
        private static void SetCache(string key, CSeriesList value)
        {
            if (null != value)  
                value.Sort(); 
            CCache.Set(key, value);    //Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
        }
        //Helper Method
        private CSeries CacheGetById(CSeriesList list)  { return list.GetById(this.SeriesId);    }
        #endregion

        #region Cloning
        public CSeries Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CSeries copy = new CSeries(this, target);
            copy.Save(txOrNull);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.SeriesId);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this.Example)
        }
        #endregion
    }
}